﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LMS.SMSH.MODEL;
using LMS.SMSH.BLL;
using System.Data.Linq;
using System.Data;



namespace LMS.SMSH.WEB
{
    public partial class AdmDeviceList : System.Web.UI.Page
    {
        int rowNo = 0;
        protected static Decimal totalCost = 0;//所有设备总价
        protected static Int32 totalDevCount = 0;//所有设备数量

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                totalCost = (Decimal)LMSDataContext.GetLMSDB.TB_DeviceInfo.Select(cost => cost.devCost).Sum();
                totalDevCount = LMSDataContext.GetLMSDB.TB_DeviceInfo.Count();

                //必须调用绑定数据的方法DataBind(),以便支持数据绑定
                this.Page.DataBind();

                //绑定gvDeiceInfo数据
                bindgvDeviceInfo();
            }
        }
        private void bindgvDeviceInfo()
        {
            gvDeviceInfo.DataSource = LMSDataContext.GetLMSDB.sp_viewDevInfo();
            gvDeviceInfo.DataKeyNames = new string[] { "devId" };
            gvDeviceInfo.DataBind();
        }
        protected void btnFind_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtDeviceName.Text.Trim()) && !string.IsNullOrWhiteSpace(txtDevVender.Text.Trim()))
            {

                string name = txtDeviceName.Text.Trim();
                string Vender = txtDevVender.Text.Trim();
                var query = LMSDataContext.GetLMSDB.TB_DeviceInfo.
                        Where(dev => dev.devName.IndexOf(name) >= 0 && dev.devVender.IndexOf(Vender) >= 0);
                gvDeviceInfo.DataSource = query;
                gvDeviceInfo.DataKeyNames = new string[] { "devId" };
                gvDeviceInfo.DataBind();
            }
            else if (string.IsNullOrWhiteSpace(txtDeviceName.Text.Trim()) && !string.IsNullOrWhiteSpace(txtDevVender.Text.Trim()))
            {
                string Vender = txtDevVender.Text.Trim();
                var query = LMSDataContext.GetLMSDB.TB_DeviceInfo.
                        Where(dev => dev.devVender.IndexOf(Vender) >= 0);
                gvDeviceInfo.DataSource = query;
                gvDeviceInfo.DataKeyNames = new string[] { "devId" };
                gvDeviceInfo.DataBind();
            }
            else if (!string.IsNullOrWhiteSpace(txtDeviceName.Text.Trim()) && string.IsNullOrWhiteSpace(txtDevVender.Text.Trim()))
            {
                string name = txtDeviceName.Text.Trim();
                var query = LMSDataContext.GetLMSDB.TB_DeviceInfo.
                        Where(dev => dev.devName.IndexOf(name) >= 0);
                gvDeviceInfo.DataSource = query;
                gvDeviceInfo.DataKeyNames = new string[] { "devId" };
                gvDeviceInfo.DataBind();
            }
            else
            {
                bindgvDeviceInfo();
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("AdmDevicePreserve.aspx");
        }


        protected void gvDeviceInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                rowNo++;
                e.Row.Cells[0].Text = (gvDeviceInfo.PageIndex * gvDeviceInfo.PageSize + rowNo).ToString();
                //鼠标悬停变色
                e.Row.Attributes.Add("onmouseover", "oldBG=this.style.backgroundColor;this.style.backgroundColor='#FF1199';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=oldBG;");


                Button myDelete = (Button)e.Row.FindControl("btnMyDelete");
                myDelete.Attributes.Add("onclick", "javascript:return confirm('确定删除吗?');");
            }
        }

        /// <summary>
        /// 全选、反选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void chkHeader_CheckedChanged(object sender, EventArgs e)
        {
            foreach (GridViewRow gr in gvDeviceInfo.Rows)
            {
                CheckBox chkItem = (CheckBox)gr.FindControl("chkItem");
                //相互变幻
                chkItem.Checked = !chkItem.Checked;
            }
        }

        protected void gvDeviceInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvDeviceInfo.PageIndex = e.NewPageIndex;
            bindgvDeviceInfo();
        }

        /// <summary>
        /// 删除选中行数据 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMyDelete_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow gr in gvDeviceInfo.Rows)
            {
                CheckBox chkItem = (CheckBox)gr.FindControl("chkItem");
                if (chkItem.Checked)
                {
                    Int32? result = 0;
                    LMSDataContext.GetLMSDB.sp_deleteDev(gr.Cells[2].Text.Trim(), ref result);
                }
            }
            bindgvDeviceInfo(); //再次绑定数据
        }



    }
}